package com.health.system.controller;

import java.util.*;

import com.health.common.constant.CacheConstants;
import com.health.common.enums.ProductStatusEnum;
import com.health.common.utils.MinIOUtils;
import com.health.common.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.health.common.annotation.Log;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.enums.BusinessType;
import com.health.system.domain.MallProduct;
import com.health.system.service.IMallProductService;
import com.health.common.utils.poi.ExcelUtil;
import com.health.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品管理Controller
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@RestController
@RequestMapping("/system/product")
public class MallProductController extends BaseController
{
    @Autowired
    private IMallProductService mallProductService;
    @Autowired
    private MinIOUtils minioUtils;

    @Value("${minio.endpoint}")
    String url;

    @Value("${minio.bucket-name}")
    String bucketName;
    @Autowired
    private IMallProductService iMallProductService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallProduct mallProduct)
    {
        startPage();
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProduct mallProduct)
    {
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        ExcelUtil<MallProduct> util = new ExcelUtil<MallProduct>(MallProduct.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(mallProductService.selectMallProductByProductId(productId));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallProduct mallProduct)
    {
        String url = mallProduct.getMainImage();
        redisTemplate.opsForSet().add(CacheConstants.TMP_VALID_FILE_SET, url);
        return toAjax(mallProductService.insertMallProduct(mallProduct));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallProduct mallProduct)
    {
        return toAjax(mallProductService.updateMallProduct(mallProduct));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(mallProductService.deleteMallProductByProductIds(productIds));
    }
    /**
     * 获取商品状态枚举列表
     */
    @GetMapping("/statusOptions")
    public AjaxResult getStatusOptions() {
        List<Map<String, String>> options = new ArrayList<>();

        for (ProductStatusEnum status : ProductStatusEnum.values()){
            Map<String, String> option = new HashMap<>();
            option.put("code", status.getCode());
            option.put("desc", status.getDesc());
            options.add(option);
        }
        return success(options);
    }

    /**
     * 上传图片
     * 修改且带productId
     */
    @PostMapping("/uploadImg/{productId}")
    public AjaxResult uploadImg(@RequestParam("file") MultipartFile file,
                                @PathVariable("productId") Long productId
    ){
        String url = iMallProductService.uploadImage(file, productId);
        return success(url);
    }

    /**
     * 上传图片
     * 新增
     */
    @PostMapping("/uploadImg")
    public AjaxResult addImg (@RequestParam("file") MultipartFile file
    ){
        String url = iMallProductService.addImg(file);
        return success(url);
    }

}
